import 'package:flutter/material.dart';
import 'package:my_flutter_tool/widgets/animated_scale.dart';

///复选框
class AppCheckBox extends StatefulWidget {
  const AppCheckBox({
    Key key,
    this.size = 18,
    this.onChange,
    this.bgColor = Colors.white,
    this.borderRadius = 2,
    this.initValue = false,
    this.borderWidth = 3,
    this.selectColor = Colors.blue,
    this.icon = Icons.check,
    this.disableColor = Colors.grey,
    this.duration = 300,
    this.curve = Curves.ease,
  }) : super(key: key);

  ///选项大小
  final double size;

  ///值变化回调
  ///为null时禁用
  final Function(bool) onChange;

  ///底色
  final Color bgColor;

  ///圆角
  final double borderRadius;

  ///初始化值,是否选中
  final bool initValue;

  ///边框大小
  final double borderWidth;

  ///选中的颜色
  final Color selectColor;

  ///图标
  final IconData icon;

  ///禁用颜色
  final Color disableColor;

  ///动画时长
  final int duration;

  ///动画曲线
  final Curve curve;

  @override
  _AppCheckBoxState createState() => _AppCheckBoxState();
}

class _AppCheckBoxState extends State<AppCheckBox> {
  ///是否选中
  bool _check = false;

  @override
  void initState() {
    super.initState();
    _check = widget.initValue;
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        if (widget.onChange != null) {
          setState(() {
            _check = !_check;
            widget.onChange(_check);
          });
        }
      },
      child: ClipRRect(
        borderRadius: BorderRadius.circular(widget.borderRadius),
        child: Stack(
          alignment: Alignment.center,
          children: [
            ///边框与背景
            Container(
              width: widget.size,
              height: widget.size,
              decoration: BoxDecoration(
                color: widget.onChange == null
                    ? widget.disableColor
                    : widget.selectColor,
                borderRadius: BorderRadius.circular(widget.borderRadius),
              ),
            ),

            ///缩放背景
            AnimatedContainer(
              decoration: BoxDecoration(
                color: widget.bgColor,
                borderRadius: BorderRadius.circular(
                    widget.borderRadius - widget.borderWidth / 2),
              ),
              width: _check ? 0 : widget.size - widget.borderWidth,
              height: _check ? 0 : widget.size - widget.borderWidth,
              duration: Duration(milliseconds: widget.duration),
              curve: widget.curve,
            ),

            ///图标
            AnimatedScale(
              scale: _check ? 1 : 0,
              duration: Duration(milliseconds: widget.duration),
              curve: widget.curve,
              child: Icon(
                widget.icon,
                size: widget.size - 4,
                color: widget.bgColor,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
